
import { imgMap} from "./resoures"
import { config } from "./config"
import { excuteScenceFun } from "./scence"
import {paintLife} from "./script"
import {Enum} from "./enum.js"

export function Logo() {
    this.img = imgMap.logoImage;
    this.paint = function () {
        ctx.drawImage(this.img, 50, 200,350,100);
    }
}

export function StartBtn() {
    this.x = 130;
    this.y = 400;

    this.img = imgMap.startImage;
    this.width = this.img.width;
    this.height = this.img.height;

    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.click = function () {
        window.changeScence('sc2')
    }
}

export function BackGroundDrawer(){
    this.x1 = 0;
    this.y1 = 0;
    
    this.paint = function (imgIndex) {
        ctx.drawImage(imgMap.bgImgAll[imgIndex], this.x1, this.y1, canvas_width, canvas_height);
    }
}

export function Sky() {
    this.x1 = 0;
    this.x2 = 0;
    this.y1 = 0;
    this.y2 = - window.canvas_height;
    //两个图片拼接展示
    this.paint = function () {
        ctx.drawImage(imgMap.bgImgAll[0], this.x1, this.y1, canvas_width, canvas_height);
        ctx.drawImage(imgMap.bgImgAll[0], this.x2, this.y2, canvas_width, canvas_height);
    }
    this.step = function () {
        this.y2 += 0.5;
        this.y1 += 0.5;
        if (this.y1 >= canvas_height) {
            this.y1 = -canvas_height;
        }
        if (this.y2 >= canvas_height) {
            this.y2 = -canvas_height;
        }
    }
    this.update = () => {
        this.paint()
        this.step()
    }

}

export function RankBtn() {
    this.x = 130;
    this.y = 500;
    this.img = imgMap.rankBtnImg;

    this.width = this.img.width;
    this.height = this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }

    this.event = function () {
        app.show_rank = true;
        fetch(api.rank, {
            method: 'get',
            credentials: 'include',
        })
            .then(response => response.json())
            .then(data => {

                app.rank = data.data
            });
    }
}

export function GameAgainBtn() {
    this.x = 150;
    this.y = 400;
    this.img = imgMap.gameagain;

    this.width = this.img.width;
    this.height = this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.click = ()=>{
        window.changeScence('sc1')
    }
}

export function ContinueBtn() {
    this.x = 130;
    this.y = 100;
    this.img = imgMap.continueGameBtnImg;

    this.width = this.img.width;
    this.height = this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.click = function () {
        excuteScenceFun('sc2','continue')
    }
}
export function EndBtn() {
    this.x = 130;
    this.y = 300;
    this.img = imgMap.endGameBtnImg;

    this.width = this.img.width;
    this.height = this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.click = ()=>{
        window.changeScence('sc4')
    }
}
export function SaveBtn() {
    this.x = 130;
    this.y = 200;
    this.img = imgMap.saveGameBtnImg;

    this.width = this.img.width;
    this.height = this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }

}
export function EndScoreBtn() {
    this.width = 400;
    this.height = 60;
    this.x = 150;
    this.y = 500;
    this.paint = function () {
        ctx.fillStyle = "red";
        ctx.font = "40px '微软雅黑'";
        ctx.textBaseline = "top";
        ctx.textAlign = "left";
        ctx.fillText("总分:" + globalData. score, this.x, this.y);
    }

}

export function Score() {
    this.paint = function () {
        var stScore = globalData.score.toString();
        ctx.drawImage(imgMap.hp, 0, 20);
        var x = 65;
        var y = 20;
        for (var i = 0; i < stScore.length; i++) {
            ctx.drawImage(imgMap.num[stScore[i] * 1], x, y);
            x += 20;
        }
    }

}

export function Level() {
    this.paint = function () {
        var stScore = globalData.level.toString();

        
        var x = 200;
        var y = 20;
        ctx.drawImage(imgMap.levViewImg, x, y, 50,30);
        for (var i = 0; i < stScore.length; i++) {
            ctx.drawImage(imgMap.num[stScore[i] * 1], x+60, y+2);
            x += 20;
        }
    }

}

export function Life() {
    this.paint = function () {
        var stLife = globalData.hero.life.toString();
        ctx.drawImage(imgMap.ufo_life, 370, 20, 30, 30);
        var x = 400;
        var y = 20;
        for (var i = 0; i < stLife.length; i++) {
            ctx.drawImage(imgMap.num[stLife[i] * 1], x, y);
            x += 20;
        }

    }
}

export function PauseBtn() {
    this.x = 10;
    this.y = canvas_height-100;
    this.img = imgMap.pauseImage;
    this.width = 50;
    this.height = 50;

    this.paint = function () {
        ctx.drawImage(imgMap.pauseImage, this.x, this.y,50,50);
    }

    this.click = function () {
        window.changeScence('sc3')
    }
}



export function eBullet(x, y, type) {
    this.x = x;
    this.y = y;
    this.img = imgMap.b[3];
    this.canDelet = false;
    this.paint = function () {
        if (type == 3) {
            this.img = imgMap.b[4];
        }
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.step = function () {
        this.y += 5;
        if (this.y >= canvas_height) {
            this.canDelete = true;
        }
    }
}
export function EBsList(enemies) {

    var makeEBsLastTime = 0
    this.eBullets = []
  
    this.make = () => {
        var eBullets = this.eBullets
        if (enemies.length == 0) {
            return;
        }
        var now = new Date().getTime();
        if (now - makeEBsLastTime <= config.makeEBsInterval) {
            return;
        }
        makeEBsLastTime = now;
        var n = parseInt(Math.random() * enemies.length);
        var e = enemies[n];
        var x = e.x + e.img.width / 2 - imgMap. b[3].width / 2;
        var y = e.y;
        eBullets[eBullets.length] = new eBullet(x, y, e.type);
        this.eBullets = eBullets

    }

    this.paintAllEBs = () => {
        for (var i = 0; i < this.eBullets.length; i++) {
            this.eBullets[i].paint()
        }
    }
    
    //让所有的敌飞机子弹移动
    this.stepALLEBs = () => {
        for (var i = 0; i < this.eBullets.length; i++) {
            this.eBullets[i].step()
        }
    }

    this.update = ()=>{
        this.make()
        this.paintAllEBs()
        this.stepALLEBs()
    }
}

Bullet.prototype.type = 0;
export function Bullet(n1, n2) {
    var hero = globalData.hero
    this.n1 = n1;
    this.n2 = n2;
    this.img = imgMap.b[Bullet.prototype.type];
    this.x = hero.x + hero.img.width / 2 - this.img.width / 2 + this.n1;
    this.y = hero.y + this.n2;
    this.canDelete = false;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.step = function () {
        this.y -= 6;
        if (this.y < -this.img.height) {
            this.canDelete = true;
        }
    }
}
export function BulletsList() {

    this.makeBulletLastTime = 0
    this.bullets = []
    this.make = () => {

        var now = new Date().getTime();
        if (now - this.makeBulletLastTime <= config.makeBulletInterval) {
            return;
        }
        this.makeBulletLastTime = now;
        var bullets = this.bullets
        var hero = globalData.hero
        //根据hero的类型做判断
        if (hero.type == 0) {
            bullets[bullets.length] = new Bullet(0, 0);
        } else if (hero.type == 1) {
            bullets[bullets.length] = new Bullet(20, 0);
            bullets[bullets.length] = new Bullet(-20, 0);
        } else if (hero.type == 2) {
            bullets[bullets.length] = new Bullet(0, 0);
            bullets[bullets.length] = new Bullet(25, 0);
            bullets[bullets.length] = new Bullet(-25, 0);
        } else if (hero.type == 3) {
            bullets[bullets.length] = new Bullet(0, 0);
            bullets[bullets.length] = new Bullet(20, 0);
            bullets[bullets.length] = new Bullet(40, 0);
            bullets[bullets.length] = new Bullet(-20, 0);
            bullets[bullets.length] = new Bullet(-40, 0);
        }
        this.bullets = bullets
        Sounds.shoot.play();
    }

    this.paintAllBullets = () => {
        for (var i = 0; i < this.bullets.length; i++) {
            this.bullets[i].paint();
        }
    }

    //让所有子弹移动
    this.stepAllBullets = () => {
        for (var i = 0; i < this.bullets.length; i++) {
            this.bullets[i].step();
        }
    }

    //删除可以删除的子弹
    this.deleteBullets = () => {
        for (var i = 0; i < this.bullets.length; i++) {
            if (this.bullets[i].canDelete) {
                this.bullets.splice(i, 1);
            }
        }
    }
    this.update = () => {
        this.deleteBullets()
        this.make()
        this.paintAllBullets()
        this.stepAllBullets()
       
    }

}

export function Bounz(x, y, type){
    this.beginTime = new Date().getTime()
    this.paint=()=>{
        if (type == 1) {
            ctx.drawImage(imgMap. bzImage, x, y)
        } else if (type == 2) {
            ctx.drawImage(imgMap. bzImage, x, y, 60, 60)
        } else if (type == 3) {
            ctx.drawImage(imgMap. bzImage, x, y, 150, 150)
        }
    }
}

export function BounzList() {

    this.list = []
    this.make = (x, y, type) => {
        this.list.push(new Bounz(x, y, type))
    }
    this.paintAllEnemies = () => {
        for (var i = 0; i < this.list.length; i++) {
            this.list[i].paint()
        }
    }
    this.deleteEnemies = () => {
        let now = new Date().getTime()
        for (var i = 0; i < this.list.length; i++) {
            if (now - this.list[i].beginTime > 200) {
                this.list.splice(i, 1);
            }
        }
    }
    this.update = () => {
        this.paintAllEnemies()
        this.deleteEnemies()
    }

}



//敌飞机 1-5 构造方法
export function Enemy1() {
    this.img = imgMap.e1[parseInt(Math.random() * imgMap.e1.length)];
    this.x = parseInt(Math.random() * (480 - this.img.width));
    this.y = -this.img.height;
    this.width = this.img.width;
    this.height = this.img.height;
    this.canDelete = false;

    this.total_life = 1;
    this.life = this.total_life;
    this.score = 5;
    this.type = 1;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
        paintLife(this)
    }
    this.step = function () {
        this.y++;
        if (this.y > 768) {
            this.canDelete = true;
        }
    }
}

//敌飞机 6-9 构造方法
export function Enemy2() {
    this.img = imgMap.e2[parseInt(Math.random() * imgMap.e2.length)];
    this.x = Math.random() * (480 - this.img.width);
    this.y = -this.img.height;
    this.width = this.img.width;
    this.height = this.img.height;
    this.canDelete = false;

    this.total_life = 1;
    this.life = this.total_life;

    this.score = 1;
    this.type = 2;
    this.paint = function () {

        ctx.drawImage(this.img, this.x, this.y);
        paintLife(this)

    }
    this.step = function () {
        this.y++;
        if (this.y > 768) {
            this.canDelete = true;
        }
    }
}

//敌飞机 boss 构造方法
export function Enemy3() {
    this.img = imgMap.e3[0];
    this.x = Math.random() * (480 - this.img.width);
    this.y = -this.img.height;
    this.width = this.img.width;
    this.height = this.img.height;
    this.canDelete = false;

    this.total_life = 2;
    this.life = this.total_life;
    this.score = 20;
    this.d = "left";
    this.type = 3;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
        paintLife(this)


    }
    this.step = function () {
        if (this.y < 200) {
            this.y++;
        } else {
            //支持左右移动
            if (this.d == "left") {
                this.x--;
                if (this.x <= 0) {
                    this.d = "right"
                }
            }
            if (this.d == "right") {
                this.x++;
                if (this.x >= 480 - this.img.width) {
                    this.d = "left"
                }
            }

        }

    }
}


export function EnemyList() {

    this.enemies = []
    this.makeEnemyLastTime = 0
    this.make = () => {
        var now = new Date().getTime();

        if (now - this.makeEnemyLastTime <= config.makeEnemyInterval) {
            return;
        }

        this.makeEnemyLastTime = now;
        var n = parseInt(Math.random() * 10);
        if (n < 6) {
            this.enemies[this.enemies.length] = new Enemy2();
        } else if (n <= 8) {
            this.enemies[this.enemies.length] = new Enemy1();
        } else {
            this.enemies[this.enemies.length] = new Enemy3();
        }
    }
    this.stepAllEnemies = () => {
        for (var i = 0; i < this.enemies.length; i++) {
            this.enemies[i].step()
        }
    }
    this.paintAllEnemies = () => {
        for (var i = 0; i < this.enemies.length; i++) {
            this.enemies[i].paint()
            paintLife(this.enemies[i])
        }
    }
    this.deleteEnemies = () => {
        for (var i = 0; i < this.enemies.length; i++) {
            if (this.enemies[i].canDelete) {
                this.enemies.splice(i, 1);
            }
        }
    }
    this.update = () => {
        this.make()
        this.paintAllEnemies()
        this.stepAllEnemies()
        this.deleteEnemies();
    }

}


export function HeroBoom(gapX, gapY, hero) {
    var n = 1
    var gapTime = 30
    var lastTime = 0

    this.gapX = gapX //相对hero原点的距离
    this.gapY = gapY //相对hero原点的距离
    this.hero = hero
    this.canDelete = false
    this.img = imgMap.heroBoomImg[n]
    this.imgW = this.img.width * 0.4
    this.imgH = this.img.height * 0.4
    this.paint = () => {
        if (n > 11) {
            this.canDelete = true
            return
        }
        this.img = imgMap.heroBoomImg[n]
        ctx.drawImage(this.img, hero.x + this.gapX - this.imgW/2, hero.y + this.gapY, this.imgW, this.imgH);
    
        var now = new Date().getTime();
        if (now - lastTime > gapTime) {
            n++
            lastTime = now
        }
    }
}


//英雄机的构造方法
export function Hero() {

    this.x = 200;
    this.y = 600;
    this.name = '战机';
    var getdata = localStorage.getItem("hero_data");
    // if (getdata) {
    //     getdata = JSON.parse(getdata);
    //     this.life = getdata.life;
    //     this.bulletType = getdata.bulletType;
    //     this.type = getdata.type;
    //     score = getdata.score ? getdata.score : 0;
    //     level = getdata.level ? getdata.level : 1;
    // } else {
    this.life = 30;
    this.bulletType = 1;
    this.type = 0; //一次能发射几个子弹

    // }

    this.img = imgMap.h[this.type];
    this.width = this.img.width;
    this.height = this.img.height;

    this.boomList = []
    this.boom = (x,y) => {
        console.log(x,y)
        this.boomList.push(new HeroBoom(x,y,this))
    }


    this.paint = function () {
        this.img = imgMap.h[this.type];
        ctx.drawImage(this.img, this.x, this.y);
        ctx.fillStyle = "red";
        ctx.font = "13px '微软雅黑'";
        ctx.textAlign = "center";
        ctx.fillText(`${this.name} LV.${this.type}` , this.x + this.img.width / 2, this.y + this.img.height + 10);

        for (var i = 0; i < this.boomList.length; i++) {
            this.boomList[i].paint()
            if (this.boomList[i].canDelete) {
                this.boomList.splice(i, 1);
            }
        }

    }
}


export function Ufo() {
    this.type = parseInt(Math.random() * 4);
    this.canDelete = false;
    if (this.type == Enum.UFO_TYPE.LIFE) {
        this.img = imgMap.ufo_life;
    } else if (this.type == Enum.UFO_TYPE.BULLET) {
        this.img = imgMap.ufo_b;
    } else if (this.type == Enum.UFO_TYPE.BAOHU) {
        this.img = imgMap.ufo_baohu;
    } else {
        this.img = imgMap.ufo_hero;
    }
    this.x = Math.random() * (480 - this.img.width);
    this.y = -this.img.height;
    this.paint = function () {
        ctx.drawImage(this.img, this.x, this.y);
    }
    this.step = function () {
        this.y += 1;
        if (this.y >= 768) {
            this.canDelete = true;
        }
    }
}

export function UfoList(){
    this.ufos = []
     var makeUfoLastTime = 0
    this. makeUfo=() =>{
       
        var now = new Date().getTime();
        if (now - makeUfoLastTime <=config.makeUfoInterval) {
            return;
        }
        makeUfoLastTime = now;
        this.ufos[this.ufos.length] = new Ufo();
    }
    this.deleteUfo=()=> {
        for (var i = 0; i < this.ufos.length; i++) {
            if (this.ufos[i].canDelete) {
                this.ufos.splice(i, 1);
            }
        }
    }
    this. paintUfo=()=> {
        for (var i = 0; i < this.ufos.length; i++) {
            this.ufos[i].paint();
        }
    }
    this. stepUfo = () =>{
        for (var i = 0; i < this.ufos.length; i++) {
            this.ufos[i].step();
        }
    }
    this.update = ()=>{
        this.makeUfo()
        this.paintUfo()
        this.stepUfo()
        this.deleteUfo()
    }
}

//保护罩构造方法
export function Bhq(hero) {
    this.imgs = imgMap.bhqs;
    this.x = hero.x - ((this.imgs[0].width - hero.img.width) / 2);
    this.y = hero.y - ((this.imgs[0].height - hero.img.height) / 2);
    
    var maxTime = 5000; //展示的总时长

    this.start = new Date().getTime();
    this.startBhq = true;
    this.interval = 100;
    this.lastTime = 0;

    this.n = 0; //当前要展示的图片序号
    this.paint = function () {
        if (this.startBhq) {
            ctx.drawImage(this.imgs[this.n], this.x, this.y);
            var now = new Date().getTime();
            if (now - this.lastTime >= this.interval) {
                this.n++;
                this.lastTime = now;
            }
            if (this.n >= this.imgs.length) {
                this.n = 0;
            }

            if (now - this.start >= maxTime) {
                this.startBhq = false;
            }
        }

    }
}